package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.CreditAccount;
import com.ruoyi.bizsys.domain.CustBankCard;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.ICreditAccountService;
import com.ruoyi.bizsys.service.ICustBankCardService;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.business.service.IBLoanInfoService;
import com.ruoyi.business.service.IBRepaymentService;
import com.ruoyi.business.service.IRepaymentService;
import com.ruoyi.business.service.QuickRepayService;
import com.ruoyi.business.service.impl.BLoanInfoServiceImpl;
import com.ruoyi.business.service.impl.RepaymentServiceImpl;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author wqg
 * @description
 * @date 2022/3/30
 */
@Slf4j
@RestController
public class CollectionRepaymentController {

    @Autowired
    IBRepaymentService bRepaymentService;
    @Autowired
    IBLoanInfoService bLoanInfoService;
    @Autowired
    private ILoanIouService loanIouService;
    @Autowired
    private ICreditAccountService creditAccountService;
    @Autowired
    ICustBankCardService custBankCardService;
    @Autowired
    IRepaymentService repaymentService;
    @Autowired
    AsyncService asyncService;
    @Autowired
    QuickRepayService quickRepayService;
    @Autowired
    ICustomerInfoService customerInfoService;
    @RequestMapping("/forcollection/collection/call")
    @Log(title = "催收还款处理", businessType = BusinessType.GET)
    public RepaymentOrder collectionCall(String contrNbr, BigDecimal realActAmt,
                                         Long loanIouId,Long createAccountId,Long bankCardId) throws Exception {
        log.info("wqg:调用业务系统进行查询---",contrNbr);
        //查询是否有在途的扣款
        RepaymentOrder resultOrder = null;
        int hasInRepaymentCount = bRepaymentService.getInRepaymentCountByLoan(contrNbr);
        if (hasInRepaymentCount > 0) {//存在在途扣款申请
            throw new Exception("存在在途扣款");
        } else {
            LoanIou loanIou = loanIouService.getById(loanIouId);
            CreditAccount creditAccount = creditAccountService.getById(createAccountId);
            CustBankCard bankCard = custBankCardService.getById(bankCardId);
            resultOrder =repaymentService.doCollectionRepaymentAction(loanIou, creditAccount, bankCard, realActAmt);
            //resultOrder = bLoanInfoService.doCollectionRepaymentAction(loanIou, creditAccount, bankCard, realActAmt);
            if (null == resultOrder) {
                throw new Exception("代扣订单生成异常");
            }
            return resultOrder;
        }
    }

    /**
     * 催收调用客服再调业务系统：处理催收钱包代扣
     * @param loanSerialNo
     * @return
     * @throws Exception
     */
    @RequestMapping("/forcollection/collection/walletpay")
    @Log(title = "催收还款处理", businessType = BusinessType.GET)
    public JSONObject walletpay(String loanSerialNo) throws Exception {
        log.info("处理催收钱包代扣:{}",loanSerialNo);
        JSONObject res = quickRepayService.collectionWalletRepay(loanSerialNo);
        log.info("处理催收钱包代扣结果:{}",res.toJSONString());
        return res;
    }
    /**
     * 催收调用客服再调业务系统：处理催收钱包购买延期
     * @param loanSerialNo
     * @return
     * @throws Exception
     */
    @RequestMapping("/forcollection/collection/walletpayForDefer")
    @Log(title = "处理催收钱包购买延期", businessType = BusinessType.GET)
    public JSONObject walletpayForDefer(String loanSerialNo,String deferredId) throws Exception {
        log.info("处理催收钱包购买延期:{}",loanSerialNo);
        JSONObject res = quickRepayService.collectionWalletBuyDefer(loanSerialNo,deferredId);
        log.info("处理催收钱包购买延期 res:{}",res.toJSONString());
        return res;
    }
    @RequestMapping("/forcollection/collection/sendExemptionSms")
    @Log(title = "处理催发送催收减免成功短信", businessType = BusinessType.GET)
    public void sendExemptionSms(String custNo,String collectAmt,String exemptionAmt) throws Exception {
        BigDecimal collectAmtb = new BigDecimal(collectAmt);
        BigDecimal exemptionAmtb = new BigDecimal(exemptionAmt);
        log.info("处理催发送催收减免成功短信:{}",custNo);
        String userId = customerInfoService.getUserIdByCustNo(custNo);
        asyncService.sendExemptionSms(userId,collectAmtb,exemptionAmtb);
    }



}
